<template>
  <div class="bottom-menu">
    <div class="all" @click="allSelect">
      <div class="select-all">
        <CheckButton class="sel" :is-checked="isSelectAll" />
      </div>
      <span>全选</span>
    </div>
    <span class="total-price">
      合计: ¥ <span>{{ totalPrice }}</span>
    </span>
    <span class="buy-product">去计算( {{ totalCount }} )</span>
  </div>
</template>

<script>
import CheckButton from 'components/common/checkButton/CheckButton'

import { mapGetters } from 'vuex'
export default {
  name: 'CartBottomBar',
  components: {
    CheckButton
  },
  data() {
    return {}
  },
  methods: {
    allSelect() {
      //全选的情况,直接反选
      if (this.isSelectAll) {
        this.cartList.forEach(item => (item.isChecked = false))
      } else {
        this.cartList.forEach(item => (item.isChecked = true))
      }
    }
  },
  computed: {
    ...mapGetters(['cartLength', 'cartList']),
    // 总价格
    totalPrice() {
      return this.cartList
        .filter(item => item.isChecked)
        .reduce((prev, val) => prev + val.count * val.price, 0)
        .toFixed(2)
    },
    // 总数量
    totalCount() {
      return this.cartList
        .filter(item => item.isChecked)
        .reduce((prev, i) => prev + i.count, 0)
    },
    // 全选
    isSelectAll() {
      // 没有商品的时候,就不用选中了
      if (this.cartLength === 0) return false
      // every方法是遍历数组,查看所有的元素是否全部符合条件,有一个不符合就停止遍历,返回false
      // 查看是否全部选中,有一个不选,就返回false,不全选
      return this.cartList.every(item => item.isChecked == true)

      // some方法是查找符合条件的,有就直接停止遍历,返回true
      // 查找是否有没选中的,有,就返回true,然后取反,不全选
      // return !this.cartList.some(item => item.isChecked == false)
    }
  }
}
</script>

<style type="text/css" scoped>
.bottom-menu {
  display: flex;
  position: relative;
  height: 44px;

  line-height: 44px;
  font-size: 14px;
  color: #888;
  background-color: #eee;
  justify-content: space-between;
  box-shadow: 0 -2px 3px rgba(0, 0, 0, 0.2);
}

.all {
  flex: 1;
  display: flex;
}
.select-all {
  width: 35px;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-content: center;
}
.sel {
  margin-top: 12px;
}
.sel img {
  margin-bottom: 13px;
}
.bottom-menu .total-price {
  font-size: 16px;
  color: #666;
  flex: 2;
}
.total-price span {
  font-weight: bold;
  color: red;
}

.bottom-menu .buy-product {
  color: #fff;
  width: 100px;
  height: 44px;
  text-align: center;
  line-height: 44px;
  background-color: orangered;
}
</style>
